var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    secKillGoods: [],
    page: 1,
    nomore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      marginTop: app.globalData.navbarHeight + app.globalData.statusBarHeight
    })
    this.getList();
  },

  getList: function () {
    var r = wx.getStorageSync("token"), that = this;
    if (that.data.nomore) {
      return;
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
      success: (result) => {

      },
      fail: () => { },
      complete: () => { }
    });

    wx.request({
      url: app.apiUrl("index/getSeckillGoods"),
      data: {
        page: that.data.page,
        pageSize: 8
      },
      method: "post",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": r
      },
      success: function (t) {
        wx.hideLoading();
        if (t.data.data.length == undefined || t.data.data.length == 0) {
          that.setData({
            noMore: true
          });
          return;
        }
        var page = that.data.page;
        if (page == 1) {
          that.setData({
            secKillGoods: t.data.data
          });
        } else {
          var secKillGoods = that.data.secKillGoods
          that.setData({
            secKillGoods: secKillGoods.concat(t.data.data)
          });
        }
      }
    });
  },
  addGoodsToCart: function (e) {
    var id = e.currentTarget.dataset.id;
    var gid = e.currentTarget.dataset.gid;
    var type = e.currentTarget.dataset.type;

    var token = wx.getStorageSync("token");
    if (!token) {
      wx.showModal({
        title: '提示',
        content: '请登录后继续',
        showCancel: false,
        success: function () {

          wx.switchTab({
            url: '../user/index',
          })
        }

      })
      return;
    }
    wx.request({
      url: app.apiUrl("index/addGoodsToCart"),
      method: "post",
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": token
      },
      data: {
        id: id,
        gid: gid,
        type: type
      },
      success: function (res) {
        if (res.data.code == 0) {
          app.setCartNum();
          wx.showToast({
            title: "加入购物车成功",
            icon: "none",
            duration: 500
          });
        } else {
          wx.showToast({
            title: res.data.data,
            icon: "none",
            duration: 500
          });
        }
      }
    });

    console.log(id, gid, type);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page;
    page++;
    this.setData({
      page: page
    });
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})